﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SYcwpb.Helpers;
using SYcwpb.ViewModels;
using System.Data;
using SYcwpb.ViewModels.DataProvider;
using SYcwpb.Controls;
using System.Collections;
using SYcwpb.Models;

namespace SYcwpb.Controls
{
    /// <summary>
    /// CrossiRoadEditor.xaml 的交互逻辑
    /// </summary>
    public partial class CrossRoadEditor : Window
    {
        private CrossRoadEditorViewModel _viewModel;

        public event EventHandler<CommitArgs> CommitEvent;

        private ArrayList CBoBoxList ;

        public CrossRoadEditor(CrossingRoadObject row,CrossRoadEditorParameter para)
        {
            InitializeComponent();

            initCBoBoxList();

            _viewModel = new CrossRoadEditorViewModel(this,row,para);

            this.DataContext = _viewModel;
        }

        public void initCBoBoxList()
        {
            CBoBoxList = new ArrayList() { this.cBoBox_1, this.cBoBox_2, this.cBoBox_3, this.cBoBox_4,
                                            this.cBoBox_5,this.cBoBox_6,this.cBoBox_7,this.cBoBox_8,};
        }

        public string GetRepTrain()
        {
            string trainID = null;
            SmallTrain index = null;
            for (int i = 0; i < CBoBoxList.Count;i ++ )
            {
                index = ((ComboBox)CBoBoxList[i]).SelectedItem as SmallTrain;
                if (index != null)
                {
                    trainID = index.TrainID;
                    break;
                }
            }

            return trainID;
        }

        public string GetRepTrainByBC()
        {
            string trainID = null;
            SmallTrain index = null;

            index = cBoBox_bcOut.SelectedItem as SmallTrain;
                if (index != null)
                {
                    trainID = index.TrainID;
                    
                }
           

            return trainID;
        }

        public SmallTrain GetOffTrain()
        {
            SmallTrain index = null;
            SmallTrain tem = null;
            for (int i = 0; i < CBoBoxList.Count; i++)
            {
                tem = ((ComboBox)CBoBoxList[i]).SelectedItem as SmallTrain;
                if (tem != null)
                {
                    index = tem;
                }
            }

            return index;
        }

        public void SetBcOutTrain(string trainID)
        {
            if (string.IsNullOrEmpty(trainID))
                return;

            this.cBoBox_bcOut.SelectedItem = GetTrain(trainID);
        }

        public void SetBcBcakTrain(string trainID)
        {
            if (string.IsNullOrEmpty(trainID))
                return;

            this.cBoBox_bcBack.SelectedItem = GetTrain(trainID);
        }

        public void SetTrain(string stringRunTrain)
        {
            if (string.IsNullOrEmpty(stringRunTrain))
                return;

            string[] trainArr = stringRunTrain.Split(new string[] { "--->" }, StringSplitOptions.None);
            if (trainArr == null || trainArr.Length < 1)
            {
                return;
            }

            int count = trainArr.Length > CBoBoxList.Count ? CBoBoxList.Count : trainArr.Length;
            int errCount = 0;

            for (int i = 0; i < count; i ++ )
            {
                try
                {
                    string trainID = trainArr[i].Substring(0, 5);
                    ((ComboBox)CBoBoxList[i - errCount]).SelectedItem = GetTrain(trainID);
                }
                catch (System.Exception ex)
                {
                    errCount++;
                	continue;
                }
            }
        }

        public SmallTrain GetFirstTrain()
        {
            SmallTrain firstT = null;

            for (int i = 0; i < CBoBoxList.Count; i ++ )
            {
                ComboBox combobox = CBoBoxList[i] as ComboBox;
                firstT = combobox.SelectedItem as SmallTrain;

                if (null == firstT)
                    continue;
                else
                    break;
            }

            return firstT;
        }
        public List<SmallTrain> GetTrainList()
        {
            SmallTrain index = null;
            List<SmallTrain> list = new List<SmallTrain>();
            for (int i = 0; i < CBoBoxList.Count; i++)
            {
                ComboBox combobox = CBoBoxList[i] as ComboBox;
                index = combobox.SelectedItem as SmallTrain;
                if (index == null)
                    continue;

                list.Add(index);
            }
            return list;
        }
        public string GetMiles()
        {
            double miles = 0;
            SmallTrain index = null;
            List<SmallTrain> list = new List<SmallTrain>();
            for (int i = 0; i < CBoBoxList.Count; i++)
            {
                ComboBox combobox = CBoBoxList[i] as ComboBox;
                index = combobox.SelectedItem as SmallTrain;
                if (index == null)
                    continue;

                list.Add(index);
            }

            if (list.Count < 1)
                return miles.ToString();

            if (CONSTDATA.UserModel.Line == "1")
            {
                miles = list.Count * _viewModel._para._totalMiles;

                if (_viewModel.AttenPlace == "车辆段")
                    miles += _viewModel._para._outMiles;
                else if (_viewModel.AttenPlace == "启工街站" || _viewModel.AttenPlace == "青年大街站")  //中途站出勤的情况
                {
                    if (!_viewModel.Cbox_BianCheng)
                    {
                        if (Convert.ToInt32(list[0].TrainID) % 2 == 1)  //基数趟车
                            miles -= _viewModel._para._downToChangedMiles;
                        else
                            miles -= _viewModel._para._upToChangedMiles;
                    }
                }

                if (_viewModel.OffPlace == "车辆段")
                    miles += _viewModel._para._inMiles;
                else if (_viewModel.OffPlace == "启工街站" || _viewModel.OffPlace == "青年大街站")
                {
                    if (Convert.ToInt32(list[list.Count - 1].TrainID) % 2 == 1)  //基数趟车
                        miles -= _viewModel._para._upToChangedMiles;
                    else
                        miles -= _viewModel._para._downToChangedMiles;
                }
            }
            else if (CONSTDATA.UserModel.Line == "2")
            {
                if (_viewModel.AttenPlace == "车辆段")
                    miles += _viewModel._para._outMiles;
                if (_viewModel.OffPlace == "车辆段" )
                    miles += _viewModel._para._inMiles;

                for (int i = 0; i < list.Count; i++)
                {
                    SmallTrain train = list[i];
                    miles += getline2miles(train.StartPlace, train.EndPlace);
                }

                if (_viewModel.AttenPlace == "青年大街站")
                {
                    if (Convert.ToInt32(list[0].TrainID) % 2 == 1)  //基数趟车
                        miles -= _viewModel._para._downToChangedMiles;
                    else
                    {
                        if (list[0].StartPlace == AttendOutPlace.STZSta)
                            miles -= _viewModel._para._upToChangedMiles;
                        else
                            miles -= _viewModel._para._hKHUpToChangedMiles;
                    }

                }

                if (_viewModel.OffPlace == "青年大街站")
                {
                    if (Convert.ToInt32(list[list.Count - 1].TrainID) % 2 == 1)  //基数趟车
                    {
                        if (list[list.Count - 1].EndPlace == AttendOutPlace.STZSta)
                            miles -= _viewModel._para._upToChangedMiles;
                        else
                            miles -= _viewModel._para._hKHUpToChangedMiles;
                    }
                    else
                        miles -= _viewModel._para._downToChangedMiles;
                }
            }
            
            return miles.ToString();
        }

        public double getline2miles(AttendOutPlace startP, AttendOutPlace endP)
        {
            if (startP == AttendOutPlace.Garage || startP == AttendOutPlace.FirSta)
            {
                if (endP == AttendOutPlace.MidSta)
                    return _viewModel._para._downToChangedMiles;
                else if (endP == AttendOutPlace.STZSta)
                    return _viewModel._para._totalMiles;
                else if (endP == AttendOutPlace.LastSta)
                    return _viewModel._para._hKHTotalMiles;
            }
            else if (startP == AttendOutPlace.STZSta)
            {
                if (endP == AttendOutPlace.Garage || endP == AttendOutPlace.FirSta)
                    return _viewModel._para._totalMiles;
                if (endP == AttendOutPlace.MidSta)
                    return _viewModel._para._upToChangedMiles;
            }
            else if (startP == AttendOutPlace.LastSta)
            {
                if (endP == AttendOutPlace.Garage || endP == AttendOutPlace.FirSta)
                    return _viewModel._para._hKHTotalMiles;
                else if (endP == AttendOutPlace.MidSta)
                    return _viewModel._para._hKHUpToChangedMiles;
            }

            return 0;
        }

        public string GetTrainList(string attenP ,string offP)
        {
            SmallTrain index = null;
            List<SmallTrain> list = new List<SmallTrain>();
            for (int i = 0; i < CBoBoxList.Count;i++ )
            {
                ComboBox combobox =  CBoBoxList[i] as ComboBox;
                index = combobox.SelectedItem as SmallTrain;
                if (index == null)
                    continue;

                list.Add(index);
            }
            return CrossingRoadViewModel.GetTrainListString(list,attenP,offP);
        }



        private SmallTrain GetTrain(string trainID)
        {
            SmallTrain train = null;
            SmallTrain index = null;
            for (int i = 0; i < CrossingRoadViewModel.TrainList.Count; i ++ )
            {
                index = CrossingRoadViewModel.TrainList[i] as SmallTrain;
                if (index.TrainID == trainID)
                {
                    train = index;
                    break; ;
                }
            }

            return train;
        }

        public string GetTime(string ctlName)
        {
            TimeInput.UserControl1 item = GetCurrentTimeCtl(ctlName);

            int times = 0;

            if (null != item)
            {
                times = (int)item.GetTimeSecond(); 
            }

            string str = GlobalVariables.FormatTime(times.ToString());

            return str;
        }

        public int GetTimeInt(string ctlName)
        {
            TimeInput.UserControl1 item = GetCurrentTimeCtl(ctlName);

            int times = 0;

            if (null != item)
            {
                times = (int)item.GetTimeSecond();
            }

            return times;
        }

        public void SetTime(string ctlName, string time)
        {
            if (string.IsNullOrEmpty(time) || time == "-1")
            {
                return;
            }
            DateTime t = Convert.ToDateTime(time);
            if (t == null)
                return;

            TimeInput.UserControl1 item = GetCurrentTimeCtl(ctlName);

            if (null != item)
            {
                item.InitScrollBarHour = (uint)t.Hour;
                item.InitScrollBarMin = (uint)t.Minute;
                item.InitScrollBarSec = (uint)t.Second;
            }
        }

        private TimeInput.UserControl1 GetCurrentTimeCtl(string ctlName)
        {
            TimeInput.UserControl1 item = null;

            if (ctl_planTime.Name == ctlName)
            {
                item = ctl_planTime;
            }
            else if (ctl_trainTime.Name == ctlName)
            {
                item = ctl_trainTime;
            }
            else if (ctl_offTime.Name == ctlName)
            {
                item = ctl_offTime;
            }

            return item;
        }

        public void OnCommited(CrossingRoadObject row)
        {
            EventHandler<CommitArgs>handler = this.CommitEvent;
            if (null != handler)
            {
                CommitArgs e = new CommitArgs(row);
                handler(this, e);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
    }

    public class CommitArgs:EventArgs
    {
        public CrossingRoadObject RowInfo {get;set;}

        public CommitArgs(CrossingRoadObject row) { this.RowInfo = row; }
    }

    public class CrossRoadEditorParameter
    {
        public string _garageAhead { get; set; }
        public string _garageBCAhead { get; set; }
        public string _normalAhead { get; set; }

        public int _garageDelay { get; set; }
        public int _normalDelay { get; set; }

        public double _outMiles = 0;
        public double _inMiles = 0;
        public double _totalMiles = 0;
        public double _upToChangedMiles = 0;
        public double _downToChangedMiles = 0;
        public double _hKHUpToChangedMiles = 0;
        public double _hKHTotalMiles = 0;

        public static CrossRoadEditorParameter CreateCrossRoadEditorParameter(string garaAhead,
            string garabcAhead,
            string normalAhead,
            int garageDelay,
            int normalDelay,
            double outMiles,
            double inMiles,
            double totalMiles,
            double upToChangedMiles,
            double downToChangedMiles,
            double hKHUpToChangedMiles,
            double hKHTotalMiles 
            )
        {
            return new CrossRoadEditorParameter()
            {
                _garageAhead = garaAhead,
                _garageBCAhead = garabcAhead,
                _normalAhead = normalAhead,
                _garageDelay = garageDelay,
                _normalDelay = normalDelay,
                _outMiles = outMiles,
                _inMiles = inMiles,
                _totalMiles = totalMiles,
                _upToChangedMiles = upToChangedMiles,
                _downToChangedMiles = downToChangedMiles,
                _hKHUpToChangedMiles = hKHUpToChangedMiles,
                _hKHTotalMiles = hKHTotalMiles
            };
            
        }
    }
}
